use byteorder::{ByteOrder, NetworkEndian};
use bytes::{BufMut, BytesMut};
use crate::defs::PayloadFormat;

//////////////////////////////////////////////

///
/// 整车数据Frame - Chapter B.3.5.3.1
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct VehicleDataFrame {
    /// 车辆状态
    pub running_state: u8,
    /// 充电状态
    pub charge_state: u8,
    /// 运行模式
    pub running_mode: u8,
    /// 车速
    pub speed: u16,
    /// 累计里程
    pub record_mileage: u32,
    /// 总电压
    pub total_voltage: u16,
    /// 总电流
    pub total_current: u16,
    /// soc
    pub soc: u8,
    /// DC-DC状态
    pub dcdc_state: u8,
    /// 档位
    pub gear: u8,
    /// 绝缘电阻 insulation resistance
    pub insulation_res: u16,
    /// 加速踏板行程值(油门)
    pub accelerator_pedal: u8,
    /// 制动踏板状态(刹车)
    pub brake_pedal: u8,
}

impl PayloadFormat for VehicleDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());
        b.put_u8(self.running_state);
        b.put_u8(self.charge_state);
        b.put_u8(self.running_mode);

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.speed);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 4];
        NetworkEndian::write_u32(&mut buf, self.record_mileage);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.total_voltage);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.total_current);
        b.put_slice(buf.as_slice());

        b.put_u8(self.soc);
        b.put_u8(self.dcdc_state);
        b.put_u8(self.gear);

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.insulation_res);
        b.put_slice(buf.as_slice());

        b.put_u8(self.accelerator_pedal);
        b.put_u8(self.brake_pedal);
        // log::info!("VehicleDataFrame is {:?}",b);
        b
    }
    fn get_len(&mut self) -> usize {
        (1 + 1 + 1 + 2 + 4 + 2 + 2 + 1 + 1 + 1 + 2 + 2) as usize
    }
}

impl VehicleDataFrame {
    pub fn new() -> Self {
        Self {
            running_state: 0,
            charge_state: 0,
            running_mode: 0,
            speed: 0,
            record_mileage: 0,
            total_voltage: 0,
            total_current: 0,
            soc: 0,
            dcdc_state: 0,
            gear: 0,
            insulation_res: 0,
            accelerator_pedal: 0,
            brake_pedal: 0,
        }
    }
}

//////////////////////////////////////////////

///
/// 定位数据Frame - Chapter 7.2.3.5 表14
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct LocationDataFrame {
    /// 定位状态
    pub loc_state: u8,
    /// 经度
    pub longitude: u32,
    /// 纬度
    pub latitude: u32,
}

impl PayloadFormat for LocationDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());
        b.put_u8(self.loc_state);

        let mut buf = [0; 4];
        NetworkEndian::write_u32(&mut buf, self.longitude);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 4];
        NetworkEndian::write_u32(&mut buf, self.latitude);
        b.put_slice(buf.as_slice());

        b
    }
    fn get_len(&mut self) -> usize {
        (1 + 4 + 4) as usize
    }
}

impl LocationDataFrame {
    pub fn update_longi(&mut self, val: f32) {
        self.longitude = (val * 10e5) as u32;
    }

    pub fn update_lati(&mut self, val: f32) {
        self.latitude = (val * 10e5) as u32;
    }
}

//////////////////////////////////////////////

///
/// 驱动电机数据Frame - Chapter 7.2.3.2 表10
///
/// 长度: 12 x N bytes
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct DriveMotorDataFrame {
    /// 电机个数 ,range: 1-253
    pub count: u8,
    /// 电机总成信息列表, 表11
    pub detail_vec: Vec<DriveMotorDataFrameItem>,
}

///
/// 电机总成信息格式
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct DriveMotorDataFrameItem {
    pub id: u8,
    pub state: u8,
    /// 电机控制器温度, range: 0 - 250, -40C - +210C
    pub controller_temp: u8,
    /// 电机转速, range: 0 - 65531
    pub speed: u16,
    /// 电机转矩, range: 0 - 65531
    pub torque: u16,
    /// 电机温度, range: 0 - 250, -40C - +210C
    pub motor_temp: u8,
    /// 电机控制器输入电压, range: 0 - 60000, 0V - 6000V
    pub controller_voltage: u16,
    /// 电机控制器直流母线电流, range: 0 - 20000, -1000A - + 1000A
    pub controller_current: u16,
}

impl PayloadFormat for DriveMotorDataFrameItem {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());

        b.put_u8(self.id);
        b.put_u8(self.state);
        b.put_u8(self.controller_temp);

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.speed);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.torque);
        b.put_slice(buf.as_slice());

        b.put_u8(self.motor_temp);

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.controller_voltage);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.controller_current);
        b.put_slice(buf.as_slice());

        b
    }
    fn get_len(&mut self) -> usize {
        (12) as usize
    }
}

impl DriveMotorDataFrameItem {
    pub fn new() -> Self {
        Self {
            id: 1,
            state: 0x03,
            controller_temp: 60,
            speed: 40000,
            torque: 50000,
            motor_temp: 90,
            controller_voltage: 2000,
            controller_current: 1000,
        }
    }

    pub fn get_size() -> usize {
        12
    }
}

impl PayloadFormat for DriveMotorDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());

        b.put_u8(self.count);
        for ditem in &self.detail_vec {
            b.put_slice(ditem.clone().serialize().as_ref());
        }
        b
    }
    fn get_len(&mut self) -> usize {
        1 + (self.count as usize) * DriveMotorDataFrameItem::get_size()
    }
}

impl DriveMotorDataFrame {
    pub fn new() -> Self {
        Self {
            count: 0,
            detail_vec: vec![],
        }
    }
}

//////////////////////////////////////////////

///
/// 燃料电池数据Frame - Chapter 7.2.3.3 表12
///
/// 长度: 18 + N bytes, N为探针总数
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct FuelCellDataFrame {
    /// 电压 ,range: 0-20000
    pub voltage: u16,
    /// 电流 ,range: 0-20000
    pub current: u16,
    /// 消耗率 ,range: 0-60000
    pub consume_rate: u16,
    /// 电池温度探针总数 ,range: 0-65531
    pub probe_count: u16,
    /// 电池温度探针值的vec
    pub probe_val_vec: Vec<u8>,

    /// 氢系统中最高温度 ,range: 0-2400
    pub gas_highest_temp: u16,
    /// 氢系统中最高温度探针代号 ,range: 1-252
    pub gas_highest_temp_probe_code: u8,
    /// 氢气最高浓度 ,range: 0-60000
    pub gas_max_concentration: u16,
    /// 氢气最高浓度探针代号 ,range: 1-252
    pub gas_max_conc_probe_code: u8,
    /// 氢气最高压力 ,range: 0-60000
    pub gas_max_pressure: u16,
    /// 氢气最高压力探针代号 ,range: 1-252
    pub gas_max_pressure_probe_code: u8,
    /// 高压DC/DC状态 , 0x01工作, 0x02断开, 0xfe异常
    pub pressure_dcdc_state: u8,
}

impl PayloadFormat for FuelCellDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.voltage);
        b.put_slice(buf.as_slice());
        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.current);
        b.put_slice(buf.as_slice());
        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.consume_rate);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.probe_count);
        b.put_slice(buf.as_slice());
        for pv in &self.probe_val_vec {
            b.put_u8(*pv);
        }
        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.gas_highest_temp);
        b.put_slice(buf.as_slice());
        b.put_u8(self.gas_highest_temp_probe_code);

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.gas_max_concentration);
        b.put_slice(buf.as_slice());
        b.put_u8(self.gas_max_conc_probe_code);

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.gas_max_pressure);
        b.put_slice(buf.as_slice());
        b.put_u8(self.gas_max_pressure_probe_code);

        b.put_u8(self.pressure_dcdc_state);

        b
    }
    fn get_len(&mut self) -> usize {
        (2 + 2 + 2 + 2 + self.probe_count + 2 + 1 + 2 + 1 + 2 + 1 + 1) as usize
    }
}

impl FuelCellDataFrame {
    pub fn new() -> Self {
        Self {
            voltage: 0,
            current: 0,
            consume_rate: 0,
            probe_count: 0,
            probe_val_vec: vec![],
            gas_highest_temp: 0,
            gas_highest_temp_probe_code: 0,
            gas_max_concentration: 0,
            gas_max_conc_probe_code: 0,
            gas_max_pressure: 0,
            gas_max_pressure_probe_code: 0,
            pressure_dcdc_state: 0,
        }
    }
}

//////////////////////////////////////////////

///
/// 发动机数据Frame - Chapter 7.2.3.4 表13
///
/// 长度: 5 bytes
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct EngineDataFrame {
    /// 发动机状态, 0x01启动, 0x02关闭, 0xfe异常
    pub state: u8,
    /// 曲轴转速,range: 0-60000
    pub crankshaft_speed: u16,
    /// 燃料消耗率 ,range: 0-60000
    pub fuel_consume_rate: u16,
}

impl PayloadFormat for EngineDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());
        b.put_u8(self.state);

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.crankshaft_speed);
        b.put_slice(buf.as_slice());
        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.fuel_consume_rate);
        b.put_slice(buf.as_slice());

        b
    }
    fn get_len(&mut self) -> usize {
        (5) as usize
    }
}

impl EngineDataFrame {
    pub fn new() -> Self {
        Self {
            state: 0,
            crankshaft_speed: 0,
            fuel_consume_rate: 0,
        }
    }
}

//////////////////////////////////////////////

///
/// 极值数据Frame - Chapter 7.2.3.6 表16
///
/// 长度: 14 bytes
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct ExtremeValueDataFrame {
    /// 最高电压电池子系统号, range: 1-250
    pub highest_voltage_subsys_id: u8,
    /// 最高电压电池单体代号, range: 1-250
    pub highest_voltage_single_id: u8,
    /// 电池单体电压最高值, range: 1-15000
    pub battery_single_voltage_highest: u16,

    /// 最低电压电池子系统号, range: 1-250
    pub lowest_voltage_subsys_id: u8,
    /// 最低电压电池单体代号, range: 1-250
    pub lowest_voltage_single_id: u8,
    /// 电池单体电压最低值, range: 1-15000
    pub battery_single_voltage_lowest: u16,

    /// 最高温度子系统号, range: 1-250
    pub highest_temperature_subsys_id: u8,
    /// 最高温度探针号, range: 1-250
    pub highest_temperature_probe_id: u8,
    /// 最高温度值, range: 0-250, -40 - +210度
    pub highest_temperature_val: u8,

    /// 最低温度子系统号, range: 1-250
    pub lowest_temperature_subsys_id: u8,
    /// 最低温度探针号, range: 1-250
    pub lowest_temperature_probe_id: u8,
    /// 最低温度值, range: 0-250, -40 - +210度
    pub lowest_temperature_val: u8,
}

impl PayloadFormat for ExtremeValueDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());
        b.put_u8(self.highest_voltage_subsys_id);
        b.put_u8(self.highest_voltage_single_id);
        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.battery_single_voltage_highest);
        b.put_slice(buf.as_slice());

        b.put_u8(self.lowest_voltage_subsys_id);
        b.put_u8(self.lowest_voltage_single_id);
        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.battery_single_voltage_lowest);
        b.put_slice(buf.as_slice());

        b.put_u8(self.highest_temperature_subsys_id);
        b.put_u8(self.highest_temperature_probe_id);
        b.put_u8(self.highest_temperature_val);

        b.put_u8(self.lowest_temperature_subsys_id);
        b.put_u8(self.lowest_temperature_probe_id);
        b.put_u8(self.lowest_temperature_val);

        b
    }
    fn get_len(&mut self) -> usize {
        (14) as usize
    }
}

impl ExtremeValueDataFrame {
    pub fn new() -> Self {
        Self {
            highest_voltage_subsys_id: 0,
            highest_voltage_single_id: 0,
            battery_single_voltage_highest: 0,
            lowest_voltage_subsys_id: 0,
            lowest_voltage_single_id: 0,
            battery_single_voltage_lowest: 0,
            highest_temperature_subsys_id: 0,
            highest_temperature_probe_id: 0,
            highest_temperature_val: 0,
            lowest_temperature_subsys_id: 0,
            lowest_temperature_probe_id: 0,
            lowest_temperature_val: 0,
        }
    }
}

//////////////////////////////////////////////

///
/// 报警数据Frame - Chapter 7.2.3.7 表17
///
/// 长度: 9 bytes + 4xN1 + 4xN2 + 4xN3 + 4xN4
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct AlarmDataFrame {
    /// 最高报警等级, range: 0-3
    pub highest_level: u8,
    /// 通用报警标志,  表18
    pub common_flag: u32,

    /// 可充电储能装置-故障总数, range: 0-252
    pub energy_storage_faults_cnt: u8,
    /// 可充电储能装置-故障代码列表, range: 4xN
    pub energy_storage_faults_vec: Vec<u32>,

    /// 驱动电机-故障总数, range: 0-252
    pub drive_motor_faults_cnt: u8,
    /// 驱动电机-故障代码列表, range: 4xN
    pub drive_motor_faults_vec: Vec<u32>,

    /// 发动机-故障总数, range: 0-252
    pub engine_faults_cnt: u8,
    /// 发动机-故障代码列表, range: 4xN
    pub engine_faults_vec: Vec<u32>,

    /// 其他-故障总数, range: 0-252
    pub other_faults_cnt: u8,
    /// 其他-故障代码列表, range: 4xN
    pub other_faults_vec: Vec<u32>,

}

impl PayloadFormat for AlarmDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());
        b.put_u8(self.highest_level);
        let mut buf = [0; 4];
        NetworkEndian::write_u32(&mut buf, self.common_flag);
        b.put_slice(buf.as_slice());

        b.put_u8(self.energy_storage_faults_cnt);
        for item in &self.energy_storage_faults_vec {
            let mut buf = [0; 4];
            NetworkEndian::write_u32(&mut buf, *item);
            b.put_slice(buf.as_slice());
        }
        b.put_u8(self.drive_motor_faults_cnt);
        for item in &self.drive_motor_faults_vec {
            let mut buf = [0; 4];
            NetworkEndian::write_u32(&mut buf, *item);
            b.put_slice(buf.as_slice());
        }
        b.put_u8(self.engine_faults_cnt);
        for item in &self.engine_faults_vec {
            let mut buf = [0; 4];
            NetworkEndian::write_u32(&mut buf, *item);
            b.put_slice(buf.as_slice());
        }
        b.put_u8(self.other_faults_cnt);
        for item in &self.other_faults_vec {
            let mut buf = [0; 4];
            NetworkEndian::write_u32(&mut buf, *item);
            b.put_slice(buf.as_slice());
        }

        b
    }
    fn get_len(&mut self) -> usize {
        (1 + 4 + 1 + 4 * self.energy_storage_faults_cnt + 1 + 4 * self.drive_motor_faults_cnt + 1 + 4 * self.engine_faults_cnt + 1 + 4 * self.other_faults_cnt) as usize
    }
}

impl AlarmDataFrame {
    pub fn new() -> Self {
        Self {
            highest_level: 0,
            common_flag: 0,
            energy_storage_faults_cnt: 0,
            energy_storage_faults_vec: vec![],
            drive_motor_faults_cnt: 0,
            drive_motor_faults_vec: vec![],
            engine_faults_cnt: 0,
            engine_faults_vec: vec![],
            other_faults_cnt: 0,
            other_faults_vec: vec![],
        }
    }
}

//////////////////////////////////////////////

///
/// 可充电储能装置电压数据Frame - Chapter B3.5.3.8 表B.5
///
/// 长度: 1 + N bytes
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct RechargeableEnergyStorageVoltageDataFrame {
    /// 可充电储能子系统个数 ,range: 1-250
    pub subsys_count: u8,
    /// 可充电储能子系统信息列表, 表11
    pub subsys_vec: Vec<RechargeableEnergyStorageVoltageDataFrameItem>,
}

///
/// 可充电储能子系统电压信息列表项
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct RechargeableEnergyStorageVoltageDataFrameItem {
    /// 可充电储能子系统号
    pub subsys_id: u8,
    /// 可充电储能子系统电压
    pub voltage: u16,
    /// 可充电储能子系统电流
    pub current: u16,
    /// 单体电池总数
    pub single_battery_cnt: u16,
    /// 本帧起始电池序号
    pub frame_battry_start_id: u16,
    /// 本帧单体电池总数,m, range: 1-200
    pub frame_battry_cnt: u8,
    /// 单体电池电压, 2*m, 0-60000
    pub single_battery_voltage_vec: Vec<u16>,
}

impl PayloadFormat for RechargeableEnergyStorageVoltageDataFrameItem {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());

        b.put_u8(self.subsys_id);

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.voltage);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.current);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.single_battery_cnt);
        b.put_slice(buf.as_slice());

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.frame_battry_start_id);
        b.put_slice(buf.as_slice());

        b.put_u8(self.frame_battry_cnt);
        for item in &self.single_battery_voltage_vec {
            let mut buf = [0; 2];
            NetworkEndian::write_u16(&mut buf, *item);
            b.put_slice(buf.as_slice());
        }

        b
    }
    fn get_len(&mut self) -> usize {
        (1 + 2 + 2 + 2 + 2 + 1 + 2 * self.single_battery_cnt) as usize
    }
}

impl RechargeableEnergyStorageVoltageDataFrameItem {
    pub fn new() -> Self {
        Self {
            subsys_id: 0,
            voltage: 0,
            current: 0,
            single_battery_cnt: 0,
            frame_battry_start_id: 0,
            frame_battry_cnt: 0,
            single_battery_voltage_vec: vec![],
        }
    }
}

impl PayloadFormat for RechargeableEnergyStorageVoltageDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());

        b.put_u8(self.subsys_count);
        for ditem in &self.subsys_vec {
            b.put_slice(ditem.clone().serialize().as_ref());
        }
        b
    }
    fn get_len(&mut self) -> usize {
        let mut s_size = 0;
        for item in &mut self.subsys_vec {
            s_size = s_size + &item.get_len();
        }
        1 + s_size
    }
}

impl RechargeableEnergyStorageVoltageDataFrame {
    pub fn new() -> Self {
        Self {
            subsys_count: 0,
            subsys_vec: vec![],
        }
    }
}

//////////////////////////////////////////////

///
/// 可充电储能装置温度数据Frame - Chapter B3.5.3.9 表B.7
///
/// 长度: 1 + N bytes
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct RechargeableEnergyStorageTemperatureDataFrame {
    /// 可充电储能子系统个数 ,range: 1-250
    pub subsys_count: u8,
    /// 可充电储能子系统信息列表, 表11
    pub subsys_vec: Vec<RechargeableEnergyStorageTemperatureDataFrameItem>,
}

///
/// 可充电储能子系统温度信息列表项
///
/// 长度: 3 + M
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct RechargeableEnergyStorageTemperatureDataFrameItem {
    /// 可充电储能子系统号
    pub subsys_id: u8,
    /// 可充电储能温度探针个数,m
    pub probe_cnt: u16,
    /// 可充电储能子系统各温度探针的温度值, 1*m, 0-250
    pub probe_vec: Vec<u8>,
}

impl PayloadFormat for RechargeableEnergyStorageTemperatureDataFrameItem {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());

        b.put_u8(self.subsys_id);

        let mut buf = [0; 2];
        NetworkEndian::write_u16(&mut buf, self.probe_cnt);
        b.put_slice(buf.as_slice());

        for item in &self.probe_vec {
            b.put_u8(*item);
        }

        b
    }
    fn get_len(&mut self) -> usize {
        (1 + 2 + 1 * self.probe_cnt) as usize
    }
}

impl crate::data_upload::upload_pkt::RechargeableEnergyStorageTemperatureDataFrameItem {
    pub fn new() -> Self {
        Self {
            subsys_id: 0,
            probe_cnt: 0,
            probe_vec: vec![],
        }
    }
}

impl PayloadFormat for RechargeableEnergyStorageTemperatureDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());

        b.put_u8(self.subsys_count);
        for ditem in &self.subsys_vec {
            b.put_slice(ditem.clone().serialize().as_ref());
        }
        b
    }
    fn get_len(&mut self) -> usize {
        let mut s_size = 0;
        for item in &mut self.subsys_vec {
            s_size = s_size + &item.get_len();
        }
        1 + s_size
    }
}

impl RechargeableEnergyStorageTemperatureDataFrame {
    pub fn new() -> Self {
        Self {
            subsys_count: 0,
            subsys_vec: vec![],
        }
    }
}

//////////////////////////////////////////////

///
/// 自定义数据Frame - Chapter 7.2.3.8 表19
///
/// 长度: 1 + N bytes
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct UserCustomDataFrame {
    /// 数据长度 ,range: 1-250
    pub data_length: u8,
    /// 可充电储能子系统信息列表, 表11
    pub data_content: Vec<u8>,
}

impl PayloadFormat for UserCustomDataFrame {
    fn serialize(&mut self) -> BytesMut {
        let mut b = BytesMut::with_capacity(self.get_len());

        b.put_u8(self.data_length);
        b.put_slice(&self.data_content[..]);
        b
    }
    fn get_len(&mut self) -> usize {
        (1 + self.data_length) as usize
    }
}

impl UserCustomDataFrame {
    pub fn new() -> Self {
        Self {
            data_length: 0,
            data_content: vec![],
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

///
/// 上传数据单项 - 数据frame
///
#[derive(Debug, Clone)]
pub enum UploadPayloadItemFrame {
    /// 整车数据-请求
    VData(VehicleDataFrame),
    /// 驱动电机数据-请求
    DriveMotor(DriveMotorDataFrame),
    /// 燃料电池数据-请求
    FuelCell(FuelCellDataFrame),
    /// 发动机数据
    Engine(EngineDataFrame),
    /// 上报定位数据
    Location(LocationDataFrame),
    /// 极值数据
    ExtremeValue(ExtremeValueDataFrame),
    /// 告警数据
    Alarm(AlarmDataFrame),
    /// 可充电储能装置电压数据
    RechargeableEnergyStorageVoltage(RechargeableEnergyStorageVoltageDataFrame),
    /// 可充电储能装置电压数据
    RechargeableEnergyStorageTemperature(RechargeableEnergyStorageTemperatureDataFrame),
    /// 用户自定义数据
    UserCustom(UserCustomDataFrame),
    None,
}

impl PayloadFormat for UploadPayloadItemFrame {
    fn serialize(&mut self) -> BytesMut {
        let ret_bmt = BytesMut::zeroed(0);
        return match self {
            Self::VData(v) => { v.serialize() }
            Self::Location(v) => { v.serialize() }
            Self::DriveMotor(v) => { v.serialize() }
            Self::FuelCell(v) => { v.serialize() }
            Self::Engine(v) => { v.serialize() }
            Self::ExtremeValue(v) => { v.serialize() }
            Self::Alarm(v) => { v.serialize() }
            Self::RechargeableEnergyStorageVoltage(v) => { v.serialize() }
            Self::RechargeableEnergyStorageTemperature(v) => { v.serialize() }
            Self::UserCustom(v) => { v.serialize() }
            Self::None => { ret_bmt }
        };
    }

    fn get_len(&mut self) -> usize {
        return match self {
            Self::VData(v) => { v.get_len() }
            Self::Location(v) => { v.get_len() }
            Self::DriveMotor(v) => { v.get_len() }
            Self::FuelCell(v) => { v.get_len() }
            Self::Engine(v) => { v.get_len() }
            Self::ExtremeValue(v) => { v.get_len() }
            Self::Alarm(v) => { v.get_len() }
            Self::RechargeableEnergyStorageVoltage(v) => { v.get_len() }
            Self::RechargeableEnergyStorageTemperature(v) => { v.get_len() }
            Self::UserCustom(v) => { v.get_len() }
            Self::None => { 0 }
        };
    }
}

///
/// 上报数据单项 - content
/// <br>包含了单个 data frame
///
#[repr(C)]
#[derive(Debug, Clone)]
pub struct UploadPayloadItem {
    pub type_id: u8,
    pub frame: Option<UploadPayloadItemFrame>,
}
